﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Input;
using SABnzbdNET.Commands;
using SABnzbdNET.Events;
using SABnzbdNET.Model;
using SABnzbdNET.ServiceAgents;

namespace SABnzbdNET.ViewModels
{
    public class AddTaskViewModel : BaseViewModel
    {
        #region Fields and properties

        private readonly IEventAggregator eventAggregator;
        private DelegateCommand browseCommand;
        private DelegateCommand closeWindowCommand;
        private DelegateCommand uploadCommand;
        private ObservableCollection<KeyValuePair<string, string>> categories;
        private string fileName;
        private string safeFileName;
        private string newzbinId;
        private string category;
        private string script;
        private string priority;

        public ICommand BrowseCommand
        {
            get { return browseCommand ?? (browseCommand = new DelegateCommand(OpenFileDialog)); }
        }
        public ICommand CloseWindowCommand
        {
            get { return closeWindowCommand ?? (closeWindowCommand = new DelegateCommand(CloseWindow)); }
        }
        public ICommand UploadCommand
        {
            get { return uploadCommand ?? (uploadCommand = new DelegateCommand(Upload)); }
        }
        public ObservableCollection<KeyValuePair<string, string>> Categories
        {
            get { return categories; }
            set { categories = value; OnPropertyChanged("Categories"); }
        }
        public string FileName
        {
            get { return fileName; }
            set { fileName = value; OnPropertyChanged("FileName"); }
        }
        public string NewzbinId
        {
            get { return newzbinId; }
            set { newzbinId = value; OnPropertyChanged("NewzbinId"); }
        }
        public string Category
        {
            get { return category; }
            set { category = value; OnPropertyChanged("Category"); }
        }
        public string Script
        {
            get { return script; }
            set { script = value; OnPropertyChanged("Script"); }
        }
        public string Priority
        {
            get { return priority; }
            set { priority = value; OnPropertyChanged("Priority"); }
        }

        #endregion

        public AddTaskViewModel()
        {
            eventAggregator = EventAggregatorProvider.Instance;
            SetCategories();
        }

        private void SetCategories()
        {
            var temp = new ObservableCollection<KeyValuePair<string, string>>
                         {
                           new KeyValuePair<string, string>("Choose category", string.Empty)
                         };
            if (ServerInfo.Current.Categories != null)
            {
                foreach (var item in ServerInfo.Current.Categories)
                {
                    temp.Add(new KeyValuePair<string, string>(item, item));
                }
            }
            Categories = temp;
        }

        private void OpenFileDialog()
        {
            var dlg = new Microsoft.Win32.OpenFileDialog { DefaultExt = ".nzb", Filter = "Binary file (.nzb)|*.nzb" };

            // Show open file dialog box
            Nullable<bool> result = dlg.ShowDialog();

            // Process open file dialog box results
            if (result == true)
            {
                safeFileName = dlg.SafeFileName;
                if (!String.IsNullOrEmpty(safeFileName))
                    FileName = dlg.FileName;
            }
        }

        private void Upload()
        {
            if (!String.IsNullOrEmpty(safeFileName) || !String.IsNullOrEmpty(NewzbinId))
            {
                Mouse.OverrideCursor = Cursors.AppStarting;
                new ServerAgent().UploadToServer(fileName, safeFileName, NewzbinId, Category, Script, Priority,
                    () => { Mouse.OverrideCursor = null; eventAggregator.Publish(new UpdateServerInfoMessage()); });
                Application.Current.Windows[1].Close();
            }
            else
            {
                MessageBox.Show("No .nzb file has been selected for upload and no Newzbin ID has been entered."
                                + Environment.NewLine + "Please try again.", "Error in upload", MessageBoxButton.OK);
            }
        }

        private static void CloseWindow()
        {
            Application.Current.Windows[1].Close();
        }
    }
}
